
<!DOCTYPE html>
<html lang="en">
    <head>

    <title>从零开始 — ILRuntime</title>
    <meta charset="utf-8">
    <meta name="description" content="ILRuntime">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

    <link rel="stylesheet" href="/ILRuntime/public/css/page.css">

    <script src="/ILRuntime/public/js/vue.js"></script>
    <script src="/ILRuntime/public/js/jquery.js"></script>
    </head>

    <body>
        
            <nav class="nav">
    <div class="border">
        <img src="/ILRuntime/public/images/logo.png" />
        <button class="hiden-in-phone">V1.4</button>
        <button id="btn-menu" class="hiden-in-pc">菜单</button>
        <ul class="nav-link hiden-in-phone">
            <!--li>
                <form id="search-form">
                    <input type="text" id="search-query" class="search-query">
                </form>
            </li!-->
            <li><a href="https://github.com/Ourpalm/ILRuntime" class="nav-link-li">下载项目</a></li>
            <li><a href="/ILRuntime/public/v1/guide/version.html" class="nav-link-li">更新记录</a></li>
            <li><a href="/ILRuntime/public/v1/guide/contribution.html" class="nav-link-li">贡献指南</li>
            <li><a href="/ILRuntime/public/v1/guide/index.html" class="nav-link-li">教程</a></li>
            <li><a href="/ILRuntime/public/" class="nav-link-li current">首页</a></li>
        </ul>
    </div>
</nav>

<div id="container" class="container clear">
    <section class="sidebar clearfix">
    <ul>
        
            
            
                <li><h3>基础</h3></li>
            
            
            
            <li>
                <p><a href="/ILRuntime/public/v1/guide/index.html" class="sidebar-link">介绍</a></p>
            </li>
        
            
            
            
            
            <li>
                <p><a href="/ILRuntime/public/v1/guide/version.html" class="sidebar-link">更新记录</a></p>
            </li>
        
            
            
            
            
            <li>
                <p><a href="/ILRuntime/public/v1/guide/contribution.html" class="sidebar-link">贡献指南</a></p>
            </li>
        
            
            
            
                <li><h3>教程</h3></li>
            
            
            <li>
                <p><a href="/ILRuntime/public/v1/guide/tutorial.html" class="sidebar-link current">从零开始</a></p>
            </li>
        
            
            
            
            
            <li>
                <p><a href="/ILRuntime/public/v1/guide/delegate.html" class="sidebar-link">ILRuntime中使用委托</a></p>
            </li>
        
            
            
            
            
            <li>
                <p><a href="/ILRuntime/public/v1/guide/cross-domain.html" class="sidebar-link">ILRuntime中跨域继承</a></p>
            </li>
        
            
            
            
            
            <li>
                <p><a href="/ILRuntime/public/v1/guide/reflection.html" class="sidebar-link">ILRuntime中的反射</a></p>
            </li>
        
            
            
            
            
            <li>
                <p><a href="/ILRuntime/public/v1/guide/redirection.html" class="sidebar-link">CLR重定向</a></p>
            </li>
        
            
            
            
            
            <li>
                <p><a href="/ILRuntime/public/v1/guide/bind.html" class="sidebar-link">CLR绑定</a></p>
            </li>
        
            
            
            
            
            <li>
                <p><a href="/ILRuntime/public/v1/guide/litjson.html" class="sidebar-link">LitJson集成</a></p>
            </li>
        
            
            
            
            
                <li><h3>其他</h3></li>
            
            <li>
                <p><a href="/ILRuntime/public/v1/guide/il2cpp.html" class="sidebar-link">IL2CPP打包注意事项</a></p>
            </li>
        
            
            
            
            
            <li>
                <p><a href="/ILRuntime/public/v1/guide/performance-optimization.html" class="sidebar-link">ILRuntime的性能优化</a></p>
            </li>
        
            
            
            
            
            <li>
                <p><a href="/ILRuntime/public/v1/guide/principle.html" class="sidebar-link">ILRuntime的实现原理</a></p>
            </li>
        
    </ul>
</section>
    <article class="clearfix">
    <h2 id="从零开始"><a href="#从零开始" class="headerlink" title="从零开始"></a>从零开始</h2><p>欢迎您选择ILRuntime ， 根据下面教程您可以快速的开始。</p>
<h3 id="启步"><a href="#启步" class="headerlink" title="启步"></a>启步</h3><h4 id="从Unity开始"><a href="#从Unity开始" class="headerlink" title="从Unity开始"></a><strong>从Unity开始</strong></h4><p>如果你希望在Unity中使用ILRuntime，推荐的方式是直接使用ILRuntime的源代码，这样ILRuntime可以根据你的发布设置自动进行优化。</p>
<p>你需要将下列源码目录复制Unity工程的Assets目录：</p>
<ul>
<li><code>Mono.Cecil.20</code></li>
<li><code>Mono.Cecil.Pdb</code></li>
<li><code>ILRuntime</code></li>
</ul>
<blockquote>
<p>需要注意的是，需要删除这些目录里面的<code>bin</code>、<code>obj</code>、<code>Properties子目录</code>，以及<code>.csproj文件</code>。此外，由于ILRuntime使用了<code>unsafe</code>代码来优化执行效率，所以你需要在Unity中开启<code>unsafe</code>模式：</p>
</blockquote>
<ul>
<li>在<code>Assets</code>目录里建立一个名为<code>smcs.rsp</code>的文本文件</li>
<li>在<code>smcs.rsp</code>文件中加入 <code>-unsafe</code></li>
</ul>
<blockquote>
<ul>
<li>如果你使用的是<code>Unity5.4</code>及以前的版本，并且使用的编译设置是<code>.Net 2.0</code>而不是<code>.Net 2.0 Subset</code>的话，<br>你需要将上述说明中的<code>smcs.rsp</code>文件名改成<code>gmcs.rsp</code>。</li>
<li>如果你使用的是<code>Unity5.5</code>以上的版本，你需要将上述说明中的<code>smcs.rsp</code>文件名改成<code>mcs.rsp</code></li>
</ul>
</blockquote>
<h4 id="Unity3D的示例工程"><a href="#Unity3D的示例工程" class="headerlink" title="Unity3D的示例工程"></a><strong>Unity3D的示例工程</strong></h4><p>你可以在<a href="https://github.com/Ourpalm/ILRuntimeU3D" target="_blank" rel="noopener">这里</a>下载到最新的Unity实例工程，该示例是在Unity5.5下制作的。</p>
<p>里面有2个工程，其中ILRuntimeDemo是Unity的主工程。实例都在这个工程当中的各个示例场景中，这个工程是在Unity5.5下完成的，如果你使用的是其他版本的Unity，可能需要自己做一些修改。</p>
<p>HotFix_Project是热更DLL工程，请用VS2015之类的C# IDE打开和进行编译，在编译前请确保至少打开过一次Unity的主工程，如果编译依然说找不到UnityEngine等dll，请手动重新指认一下</p>
<h4 id="从Visual-Studio开始"><a href="#从Visual-Studio开始" class="headerlink" title="从Visual Studio开始"></a><strong>从Visual Studio开始</strong></h4><p>如果你希望在VisualStudio的C#项目中使用ILRuntime， 你只需要引用编译好的<code>ILRuntime.dll</code>，<code>Mono.Cecil.20.dll</code>以及<code>Mono.Cecil.Pdb</code>即可。</p>
<h3 id="使用之前"><a href="#使用之前" class="headerlink" title="使用之前"></a>使用之前</h3><p>请先执行一次测试用例以保证您下载的ILRuntime没有问题。</p>
<p>ILRuntime项目提供了一个测试用例工程ILRuntimeTest，用来验证ILRuntime的正常运行，在运行测试用例前，需要手动生成一下TestCases里面的工程，生成DLL文件。</p>
<h3 id="开始使用"><a href="#开始使用" class="headerlink" title="开始使用"></a>开始使用</h3><p>使用ILRuntime非常简单，只需要以下这些代码即可运行一个完整的例子：</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line">ILRuntime.Runtime.Enviorment.AppDomain appdomain;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Start</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    StartCoroutine(LoadILRuntime());</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">IEnumerator <span class="title">LoadILRuntime</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    appdomain = <span class="keyword">new</span> ILRuntime.Runtime.Enviorment.AppDomain();</span><br><span class="line"><span class="meta">#<span class="meta-keyword">if</span> UNITY_ANDROID</span></span><br><span class="line">    WWW www = <span class="keyword">new</span> WWW(Application.streamingAssetsPath + <span class="string">"/Hotfix.dll"</span>);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">else</span></span></span><br><span class="line">    WWW www = <span class="keyword">new</span> WWW(<span class="string">"file:///"</span> + Application.streamingAssetsPath + <span class="string">"/Hotfix.dll"</span>);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">    <span class="keyword">while</span> (!www.isDone)</span><br><span class="line">        <span class="keyword">yield</span> <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="keyword">string</span>.IsNullOrEmpty(www.error))</span><br><span class="line">        D.error(www.error);</span><br><span class="line">    <span class="keyword">byte</span>[] dll = www.bytes;</span><br><span class="line">    www.Dispose();</span><br><span class="line"><span class="meta">#<span class="meta-keyword">if</span> UNITY_ANDROID</span></span><br><span class="line">    www = <span class="keyword">new</span> WWW(Application.streamingAssetsPath + <span class="string">"/Hotfix.pdb"</span>);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">else</span></span></span><br><span class="line">    www = <span class="keyword">new</span> WWW(<span class="string">"file:///"</span> + Application.streamingAssetsPath + <span class="string">"/Hotfix.pdb"</span>);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">    <span class="keyword">while</span> (!www.isDone)</span><br><span class="line">        <span class="keyword">yield</span> <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="keyword">string</span>.IsNullOrEmpty(www.error))</span><br><span class="line">        D.error(www.error);</span><br><span class="line">    <span class="keyword">byte</span>[] pdb = www.bytes;</span><br><span class="line">    <span class="keyword">using</span> (System.IO.MemoryStream fs = <span class="keyword">new</span> MemoryStream(dll))</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">using</span> (System.IO.MemoryStream p = <span class="keyword">new</span> MemoryStream(pdb))</span><br><span class="line">        &#123;</span><br><span class="line">            appdomain.LoadAssembly(fs, p, <span class="keyword">new</span> Mono.Cecil.Pdb.PdbReaderProvider());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    OnILRuntimeInitialized();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">OnILRuntimeInitialized</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    appdomain.Invoke(<span class="string">"Hotfix.Game"</span>, <span class="string">"Initialize"</span>, <span class="literal">null</span>, <span class="literal">null</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这个例子为了演示方便，直接从StreamingAssets目录里读取了脚本DLL文件以及调试符号PDB文件， 实际发布的时候，如果要热更，肯定是将DLL和PDB文件打包到Assetbundle中进行动态加载的，这个不是ILRuntime的范畴，故不具体演示了。</p>
<h3 id="调试插件"><a href="#调试插件" class="headerlink" title="调试插件"></a>调试插件</h3><p>ILRuntime提供了一个支持Visual Studio2015和Visual Studio 2017的调试插件，用来源码级调试你的热更脚本。</p>
<p>你可以在<a href="https://github.com/Ourpalm/ILRuntime/releases" target="_blank" rel="noopener">这里</a>下载到最新的Visual Studio调试插件。</p>
<p><strong>使用方法如下：</strong></p>
<ul>
<li>安装ILRuntime调试插件，并重新启动VS2015或VS2017</li>
<li>运行Unity工程，并保证执行过appdomain.DebugService.StartDebugService(56000);来启动调试服务器</li>
<li>用VisualStudio打开热更DLL项目</li>
<li>点击菜单中的Debug-&gt;Attach to ILRuntime按钮</li>
<li>在弹出来的窗口中填入被调试的主机的IP地址以及调试服务器的端口</li>
<li>点击Attach按钮后，即可像UnityVS一样下断点调试</li>
</ul>
<p><strong>注意事项：</strong></p>
<ul>
<li>如果使用VS2015的话需要<code>Visual Studio 2015 Update3</code>以上版本</li>
</ul>

    <div class="footer">
        发现错误？想参与编辑？ 
        <a href="https://github.com/Ourpalm/ILRuntime/tree/master/docs/source/src/v1/guide/tutorial.md" target="_blank">
            在 Github 上编辑此页！
        </a>
    </div>
</article>

<div class="sub-nav hiden-in-phone">
    <dl id="sub-nav">
        <dt>本文内容</dt>
        <dd v-for="(ele, index) in sub_nav">
           <a v-bind:href="ele.href">{{ ele.name }}</a>
        </dd>
    </dl>
</div> 
</div>

<footer>
    <div>
        <p>© Copyright 2017 Ourpalm All Rights Reserved</p>
        <p>掌趣科技2017</p>
    </div>
</footer>


<script>
var vm = new Vue({
    el : '#container',
    data: {
        sub_nav : [ ]
    },
    created:function(){
        var obj = [];
        $("article h3").each(function(){
            obj.push({name :  $(this).find("a").attr("title") , href : "#"+$(this).attr("id") });
        });

        this.sub_nav = obj;       
    }
});

var isShow = false;
$("nav").on("click","#btn-menu" , function(){

    if(!isShow){
        
        if($(document).scrollTop() > $(".sidebar").height() - 100){

            $('html, body').animate({scrollTop:0} , 300, "swing",function(){

                $(".sidebar").fadeIn();
                $(".container").animate({"left" : "15rem"}, 500,"swing");
            });
        }else{

                $(".sidebar").fadeIn();
                $(".container").animate({"left" : "15rem"}, 500,"swing");

        }

    }else{
        $(".sidebar").fadeOut();
        $(".container").animate({"left" : "0rem"}, 500,"swing");
    }
    isShow = !isShow;

});

$(".container").on("click" , "article" , function(){

    if(isShow){
        $(".sidebar").fadeOut();
        $(".container").animate({"left" : "0rem"}, 500,"swing");
        isShow = false;    
    }

});
</script>
        
    </body>
</html>